<template>
  <div>
    <div class="warp-box  clearfix">
      <div class="warp-con">
        <div class="header">
          <div class="headerWrapper">
            <div class="search">
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input placeholder="请输入菜单名称" v-model="searchFrom.name" @keyup.enter.native="query" auto-complete="off">
                    <i class="el-icon-close el-input__icon" slot="suffix" @click="searchFrom.name = ''"></i>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="query()">查询</el-button>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="addDialog">新增</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
        <div class="content">
          <el-table max-height="660" :data="tableData" :header-row-style="HeaderStyle" :row-style='cellStyle' style="width: 100%"
            border row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" v-loading="loading"
            element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
            <el-table-column align="center" prop="name" show-overflow-tooltip label="名称" />
            <el-table-column align="center" prop="type" show-overflow-tooltip label="类型">
              <template slot-scope="scope">
                <el-tag :type="scope.row.type === 0 ? 'info' : 'success'" disable-transitions>
                  {{ scope.row.type | menuTypeFilter }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="icon" show-overflow-tooltip label="图标" />
            <el-table-column align="center" prop="code" show-overflow-tooltip label="标识" />
            <el-table-column align="center" prop="path" show-overflow-tooltip label="访问路径" />
            <el-table-column align="center" prop="sortNo" show-overflow-tooltip label="排序" />
            <el-table-column label="操作" align="center" min-width="130">
              <template slot-scope="scope">
                <span @click="addDialog(scope.row)" class="pointer">新增<i class="line"></i></span>
                <span @click="updateDialog(scope.row)" class="pointer">修改<i class="line"></i></span>
                <span @click="deleteMenu(scope.row)" class="pointer">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="paging">
          <el-pagination :page-size="this.pageSize" :page-sizes="[5, 8, 10]" :total="tableDataTotalCount"
                         @current-change="handleCurrentChange" @size-change="handleSizeChange" background
                         layout="total, sizes, prev, pager, next, jumper" />
        </div>
      </div>
      <el-dialog :close-on-click-modal="false" :title="detailEditType == 'edit' ? '修改菜单' : '添加菜单'" :width="'30%'"
        :before-close="cancel" :visible.sync="dialogFormVisible">
        <el-form :model="addForm" :rules="rulesAdd" ref="ruleAddForm" class="demo-ruleForm" label-width="120px">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="addForm.type" size="small" text-color="#8DC147" id="menuRadio" fill="#00FFFF">
              <el-radio-button class="radio-bg" :label="0">菜单</el-radio-button>
              <el-radio-button class="radio-bg" :label="1">按钮</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="addForm.name" class="input"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="icon" v-if="addForm.type === 0">
            <el-input v-model="addForm.icon" class="input"></el-input>
          </el-form-item>
          <el-form-item label="访问路径" prop="path" v-if="addForm.type === 0">
            <el-input v-model="addForm.path" class="input" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code" v-if="addForm.type === 1">
            <el-input v-model="addForm.code" class="input" />
          </el-form-item>
          <el-form-item label="上级菜单" prop="parentId" v-if="detailEditType == 'edit'">
            <SelectTree :options="menuOption" :value="addForm.parentId" :clearable="true" :accordion="false" @getValue="getValue($event)" />
          </el-form-item>
          <el-form-item label="排序" prop="sortNo">
            <el-input-number v-model="addForm.sortNo" :min="1" class="input"></el-input-number>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="detailEditType == 'edit'" class="dialogBtn" type="primary" @click="updateMenu('ruleAddForm')">修
            改</el-button>
          <el-button v-else type="primary" @click="addMenu('ruleAddForm')" class="dialogBtn">添 加</el-button>
          <el-button type="primary" @click="cancel" class="dialogBtn">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import menuApi from '@/api/menuApi'
  import SelectTree from '@/components/TreeSelect'

  export default {
    components: {
      SelectTree
    },
    data() {
      return {
        loading: false,
        searchFrom: {
          name: ''
        },
        // 表格样式
        cellStyle: {
          height: '60px',
          fontSize: '16px'
        },
        HeaderStyle: {
          height: '65px',
          fontSize: '16px'
        },
        tableData: [],
        pageIndex: 1,
        pageSize: 8,
        TableDataTotalCount: '',
        dialogFormVisible: false,
        detailEditType: 'detail', // 'edit'
        addForm: {},
        menuOption: [],
        rulesAdd: {
          name: [{
            required: true,
            message: '请输入菜单名称',
            trigger: 'blur'
          }],
          path: [{
            required: true,
            message: '请输入访问路径',
            trigger: 'blur'
          }]
        }
      }
    },
    computed: {
      tableDataTotalCount() {
        return this.TableDataTotalCount ? this.TableDataTotalCount : 0
      }
    },
    filters: {
      menuTypeFilter: function(val) {
        if (val === 0) {
          return '菜单'
        } else if (val === 1) {
          return '按钮'
        }
      }
    },
    created() {
      this.findMenuPage()
    },
    methods: {
      query() {
        this.pageIndex = 1;
        this.findMenuPage();
      },
      pager(val) {
        this.pageIndex = val
        this.findMenuPage()
      },
      findMenuPage() {
        this.loading = true
        menuApi.findMenuPage({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          name: this.searchFrom.name,
          sort: 'sortNo',
          order: 'ASC'
        }).then((res) => {
          this.loading = false
          let result = res.data.result
          if (res.data.message === 'success' && result) {
            this.tableData = res.data.result.rows
            this.TableDataTotalCount = res.data.result.totalCount
          } else {
            this.tableData = []
            this.TableDataTotalCount = 0
          }
        })
      },
      async findMenu() {
        let res = await menuApi.findMenu()
        if (res.data.message === 'success') {
          this.menuOption = res.data.result
        }
      },
      addDialog(row) {
        if (row) {
          this.addForm.parentId = row.id
        }
        this.$set(this.addForm, 'type', 0)
        this.detailEditType = 'add'
        this.dialogFormVisible = true
      },
      addMenu(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showLoading('正在保存')
            menuApi.addMenu(this.addForm).then((res) => {
              this.hideLoading()
              if (res.data.message === 'success') {
                this.$notify({
                  title: '消息',
                  message: ('添加成功!'),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
                this.dialogFormVisible = false
                this.addForm = {}
                this.findMenuPage()
              }
            })
          } else {
            return false
          }
        })
      },
      updateDialog(row) {
        this.findMenu()
        this.detailEditType = 'edit'
        menuApi.getMenu(row.id).then((res) => {
          if (res.data.message === 'success') {
            this.addForm = res.data.result
            this.dialogFormVisible = true
          } else {
            this.addForm = {}
          }
        })
      },
      updateMenu(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showLoading('正在修改')
            let params = this.addForm
            menuApi.updateMenu(params).then((res) => {
              this.hideLoading()
              if (res.data.message === 'success') {
                this.$notify({
                  title: '消息',
                  message: ('修改成功!'),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
                this.addForm = {}
                this.dialogFormVisible = false
                this.findMenuPage()
              }
            })
          } else {
            return false
          }
        })
      },
      deleteMenu(row) {
        let tips = ''
        let menuId = row.id
        if (row) {
          tips = `此操作将删除: ${row.name}, 是否继续?`
        } else {
          return
        }
        this.$confirm(tips, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          menuApi.deleteMenu(menuId).then((res) => {
            if (res.data.messageType === '0') {
              this.$notify({
                title: '消息',
                message: ('删除成功！'),
                position: 'bottom-right',
                showClose: false,
                type: 'success'
              })
              this.findMenuPage()
            } else {
              this.$notify.error({
                title: '消息',
                message: '删除失败'
              })
            }
          })
        }).catch((error) => {
          console.log(error)
        })
      },
      getValue(value) {
        if (this.addForm.id === value) {
          this.$message.error('不能选择自己')
        } else {
          this.addForm.parentId = value
        }
      },
      cancel() {
        this.addForm = {}
        this.menuOption = []
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.tableData = []
        this.findMenuPage()
      },
      handleCurrentChange(val) {
        this.pageIndex = val
        this.tableData = []
        this.findMenuPage()
      },
      showLoading(message) {
        this.$loading({
          lock: true,
          text: message,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      hideLoading() {
        this.$loading().close()
      }
    }
  }
</script>
<style lang="less" scoped>
  .container {
    padding-left: 94px;
    padding-right: 94px;
  }

  .search {
    & /deep/ .el-form-item__label {
      font-size: 1.8rem;
    }
  }

  /deep/ .el-transfer-panel {
    background: transparent;
  }

  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }

  .el-transfer-panel {
    background: #33a1bf;
  }

  .footer {
    margin-top: 40px;
    color: #ffffff;
    font-size: 16px;

    .info {
      .select {
        width: 52px;
        height: 35px;
      }

      .select /deep/ .el-input__inner {
        padding-left: 5px;
        padding-right: 0;
      }

      .select /deep/ .el-input__icon {
        width: 15px;
      }

      .select /deep/ .el-select__caret {
        /*transform:rotate(270deg);*/
        /*color: black;*/
      }

      .select /deep/ .el-select__caret.is-reverse {}
    }

    .paging {
      /*background-color: pink;*/
      text-align: right;
      padding-right: 30px;
      margin-top: -20px;

      & /deep/ .el-pagination.is-background .btn-prev {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination.is-background .btn-next {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pager li {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination__total,
      & /deep/ .el-pagination__sizes {
        float: left;
        font-size: 1.6rem;
        height: 35px;
        line-height: 35px;
      }

      & /deep/ .el-input__inner {
        /*width:52px;*/
        height: 35px;
      }

      & /deep/ .el-input .el-input__inner {
        padding-right: 20px;
        color: #FFFFFF;
        font-size: 1.6rem;
      }
    }
  }

  .content {
    margin-top: 10px;

    & /deep/ .el-checkbox__inner {
      height: 20px;
      width: 20px;
      font-size: 28px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);
      background: none;
    }

    & /deep/ .el-tooltip__popper {
      background-color: red;
    }

    & /deep/ .el-checkbox__inner::after {
      border-width: 2px;
      height: 6px;
      left: 6px;
      top: 3px;
      width: 4px;
    }

    & /deep/ .el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
      top: 8px;
      background: white;
    }
  }

  .line {
    display: inline-flex;
    height: 21px;
    width: 1px;
    background: rgba(0, 255, 255, 1);
    margin: 0 10px;
    vertical-align: middle;
  }

  .dialogBtn {
    font-size: 1.6rem;

    &:hover {
      background: rgba(12, 161, 151, 1);
    }
  }

  .radio-bg {
    & /deep/ .el-radio-button__inner {
      background: #025361;
      border: 1px solid #025361;
    }
  }
</style>
<style lang="less">
  /**
   * 修改样式   覆盖默认的饿了么全局样式
   */
  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }
</style>
